<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable @change="handleFilter" class="filter-item" style="width: 200px;" placeholder="请输入配方名称" />
      <el-button v-permission="['GET /admin/formula/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['POST /admin/formula/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="downloadTemplate">下载模板</el-button>
      <el-upload
        :show-file-list="false"
        :before-upload="beforeUpload"
        :on-success="onSuccess"
        :on-error="onError"
        :disabled="importDataDisabled"
        class="filter-item"
        :http-request="uploadFile"
        action="/admin/material/import">
        <el-button :disabled="importDataDisabled" v-permission="['POST /admin/material/import']" type="success" :icon="importDataBtnIcon">
          {{importDataBtnText}}
        </el-button>
      </el-upload>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.materialVOS" style="width: 100%">
            <el-table-column align="center" label="原料ID" prop="id" />
            <el-table-column align="center" label="原料编码" prop="code" />
            <el-table-column align="center" label="原料名称" prop="name" />
            <el-table-column align="center" label="数量" prop="amount" />
            <el-table-column align="center" label="单位" prop="unit" />
            <el-table-column align="center" label="单价(元)" prop="price" />
            <el-table-column align="center" label="成本金额(元)" prop="totalPrice" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column align="center" label="配方ID" prop="id" />

      <el-table-column align="center" label="配方编码" prop="code" />

      <el-table-column align="center" label="配方名称" prop="name" />

      <el-table-column align="center" label="个数" prop="number" />

      <el-table-column align="center" label="成本金额(元)" prop="price" />

      <el-table-column align="center" label="简介" prop="brief" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/formula/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['POST /admin/formula/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .goods-detail-box img {
    width: 100%;
  }
</style>

<script>
import { listFormula, deleteFormula } from '@/api/formula'
import { getToken } from '@/utils/auth'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination'
import {importFile} from "@/api/material"; // Secondary package based on el-pagination

export default {
  name: 'Formula',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      formulaDetail: '',
      detailDialogVisible: false,
      downloadLoading: false,
      importDataBtnText: '导入数据',
      importDataBtnIcon: 'el-icon-upload2',
      importDataDisabled: false,
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listFormula(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/stuff/formula-create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/stuff/formula-edit', query: { id: row.id }})
    },
    handleDelete(row) {
      deleteFormula(row)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.getList()
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['配方ID', '配方编码', '配方名称', '配方包含产品个数', '总价格', '简介']
        const filterVal = ['id', 'code', 'name', 'number', 'price', 'brief']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '配方信息')
        this.downloadLoading = false
      })
    },
    downloadTemplate() {
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = [
          '原料编码',
          '原料名称',
          '原料单位',
          '原料单价'
        ]
        const filterVal = [
          'code',
          'name',
          'unit',
          'price'
        ]
        excel.export_json_to_excel2(tHeader, [{code:"304010002",name:"白糖",unit:"kg",price:"0.0290"},{code:"304010003",name:"白纸",unit:"个",price:"0.0291"}], filterVal, '原料信息')
        this.downloadLoading = false
      })
    },
    onError(err, file, fileList) {
      this.importDataBtnText = '导入数据';
      this.importDataBtnIcon = 'el-icon-upload2';
      this.importDataDisabled = false;
    },
    onSuccess(response, file, fileList) {
      this.importDataBtnText = '导入数据';
      this.importDataBtnIcon = 'el-icon-upload2';
      this.importDataDisabled = false;
      this.getList();
    },
    beforeUpload() {
      this.importDataBtnText = '正在导入';
      this.importDataBtnIcon = 'el-icon-loading';
      this.importDataDisabled = true;
    },
    uploadFile(param) {
      this.importDataBtnText = '正在导入';
      this.importDataBtnIcon = 'el-icon-loading';
      this.importDataDisabled = true;
      const formData = new FormData()
      formData.append('file', param.file)
      importFile(formData)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.importDataBtnText = '导入数据';
          this.importDataBtnIcon = 'el-icon-upload2';
          this.importDataDisabled = false;
          this.getList();
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
          this.importDataBtnText = '导入数据';
          this.importDataBtnIcon = 'el-icon-upload2';
          this.importDataDisabled = false;
        })
    }
  }
}
</script>
